#!/usr/local/bin/perl

use strict;
use LWP::UserAgent;
use Crypt::SSLeay;
use JSON;
use HTTP::Cookies;
use HTTP::Status;
use Data::Dumper;

my $browser = LWP::UserAgent->new;
$browser->protocols_allowed( [ 'http', 'https'] );
my $cookieFile = "addama_lwp_cookies.dat";
my $cookie_jar = HTTP::Cookies->new(
    file => $cookieFile,
  );
$cookie_jar->load($cookieFile);
$browser->cookie_jar($cookie_jar);
$browser->timeout(100);
$browser->env_proxy;
open (MYFILE, 'YADYeast2.csv');
my $i = 1;
my $chopped = '';
my $strainName = '';
my $dateFrozen = '';
my $yoPostCreate = '';
my $anno;
my $json = new JSON;
my $obj;
my $count;
my $maxStrainNumber = 0;

while (<MYFILE>) {
  chomp;
  print "line $count $_\n";
  #@personal = split(/","/, $_);
  #$size = @personal;
  $yoPostCreate = '"http://ravioli-2:8080/addama-rest/dudley-sample/path/datasource/YAD/';
  processLine($_);
  $count = $count + 1;
}
close (MYFILE);
sub processLine{
  my $line = $_[0];
  my @splValues = split('","', $line);
  $i = 1;
  foreach my $val (@splValues) {
  #chop " off first value's beginning, offset of 1 to length
  if ($i == 1){
     $strainName = substr($val, 1, (length($val)-1));
     #split strain name and capture max strain number
     my @strainNameArray = split(' ', $strainName);
     my $strainNumber = $strainNameArray[1];
     if ($strainNumber > $maxStrainNumber){
        $maxStrainNumber = $strainNumber;
        print("new max strain number is: $maxStrainNumber\n");
     }
     $yoPostCreate = $yoPostCreate . $strainName .'/create?JSON={strainName:' . $strainName;
  }elsif ($i == 2) {
     $anno = ';alias:\''. $val .'\'';
     $yoPostCreate = $yoPostCreate . $anno;
  }elsif ($i == 3) {
     $anno = ';matingType:\''. $val .'\'';
     $yoPostCreate = $yoPostCreate . $anno;
  }elsif ($i == 4) {
     $anno = ';genotype:\''. $val .'\'';
     $yoPostCreate = $yoPostCreate . $anno;
  }elsif ($i == 5) {
     $anno = ';comments:\''. $val .'\'';
     $yoPostCreate = $yoPostCreate . $anno;
  }elsif ($i == 6) {
     $anno = ';contributor:\''. $val .'\'';
     $yoPostCreate = $yoPostCreate . $anno;
  }elsif ($i == 7) {
     $anno = ';boxNumber:\''. $val .'\'';
     $yoPostCreate = $yoPostCreate . $anno;
  }elsif ($i == 8) {
     $anno = ';parents:\''. $val .'\'';
     $yoPostCreate = $yoPostCreate . $anno;
  }else{
   #chop " off last value's end
   #if ($i == 9){
      #chop($val);
      #print("chopped $val\n");
      #chop($val);
      #print("chopped chopped $val\n");
      $dateFrozen = substr($val,0,(length($val)-2));
      #print("value:$i dateFrozen:$dateFrozen\n");
     $anno = ';dateFrozen:\''. $dateFrozen .'\'';
     $yoPostCreate = $yoPostCreate . $anno;
   $yoPostCreate = $yoPostCreate .'}"';
   print("POST this: $yoPostCreate\n");
   my $response = $browser->post($yoPostCreate);
      if ($response->is_success) {
          my $response = $response->content;
          # eval in case response isn't legit json
          eval { $obj = $json->decode($response) };
          #if ($@) {
          print("$yoPostCreate POSTED Successfully\n");
          #} #else {
          #  print Dumper $obj;
          #}
      }
      else {
          if($response->code()==401) {
            print "\nLogin is required, please run AddamaLogin.pl and try again.\n\n"
          } else {
            print ("POST FAILED");
            die $response->status_line;
          }
      }

   $yoPostCreate = '';
  }
   $i++;
 }
}

